import Vue from 'vue'
import Router from 'vue-router'
import login from '@/views/login'
import Cookies from 'js-cookie'
import {
  getToken
} from '@/utils/auth'
import {
  roterPre
} from '@/settings'
// 总的页面布局
import Layout from '@/layout'



/* Router Modules */

// 财务
// import accountsRouter from './modules/accounts'


// 商品
import productRouter from './modules/product'

// 设置==>权限管理
import settingRouter from './modules/setting'
// 设置==>系统配置
import configRouter from './modules/config'


Vue.use(Router)


export const constantRoutes = [

  // accountsRouter,
  productRouter,
  settingRouter,
  configRouter,

  {
    path: roterPre,
    component: Layout,
    redirect: `${roterPre}/dashboard`,
    children: [
      {
        path: `${roterPre}/dashboard`,
        // component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '控制台', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: `${roterPre}/login`,
  },
  {
    path: `${roterPre}/login`,
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // {
  //   path: roterPre,
  //   component: Layout,
  //   redirect: `${roterPre}/dashboard`,
  //   children: [{
  //     path: `${roterPre}/dashboard`,
  //     // component: () => import('@/views/dashboard/index'),
  //     name: 'Dashboard',
  //     meta: {
  //       title: '控制台',
  //       icon: 'dashboard',
  //       affix: true
  //     }
  //   }, ]
  // },
  // {
  //   path: '/',
  //   component: login,
  //   redirect: `${roterPre}/dashboard`,
  // },
  // {
  //   path: `${roterPre}/login`,
  //   component: login,
  //   hidden: true
  // },
  // {
  //   path: '/auth-redirect',
  //   component: () => import('@/views/login/auth-redirect'),
  //   hidden: true
  // },
  {
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    meta: {
      title: 'Error Pages',
      icon: '404'
    },
    children: [{
        path: '401',
        component: () => import('@/views/error-page/401'),
        name: 'Page401',
        meta: {
          title: '401',
          noCache: true
        }
      },
      {
        path: '404',
        component: () => import('@/views/error-page/404'),
        name: 'Page404',
        meta: {
          title: '404',
          noCache: true
        }
      }
    ]
  },
  {
    path: roterPre + '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: roterPre + '/setting/icons',
    component: () => import('@/components/iconFrom/index'),
    name: 'icons'
  },
  {
    path: roterPre + '/setting/uploadPicture',
    component: () => import('@/components/uploadPicture/index.vue'),
    name: 'uploadPicture'
  },
  {
    path: roterPre + '/setting/storeProduct',
    component: () => import('@/components/goodList/index.vue'),
    name: 'StoreProduct'
  },
  {
    path: roterPre + '/setting/referrerList',
    component: () => import('@/components/referrerList/index.vue'),
    name: 'ReferrerList'
  },
  // {
  //   path: '*',
  //   redirect: roterPre + '/404',
  //   hidden: true
  // }
]







const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}


// 配置路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从那个路径跳转而来
  // next 是一个函数 表示放行
  //     next()放行  next('/login') 强制跳转
  if (to.path == `${roterPre}/login`)
    return next()
  // 如果用户想要访问登陆页面直接放行，如果不是在判断是否携带token
  const hasToken = getToken()
  console.log(hasToken, "我是token")

  if (!hasToken){
    next(`${roterPre}/login`)
    window.location.reload();
    return
  }else{
    next()
  }
  

})


export default router




// // 配置跳转路径
// const routes = [
//   { path: '/', redirect:'/login'},
//   { path: '/login', component: login },
//   {
//     path: roterPre,
//     component: Layout,
//     redirect: `${roterPre}/dashboard`,
//     children: [
//       {
//         path: `${roterPre}/dashboard`,
//         component: () => import('@/views/dashboard/index'),
//         name: 'Dashboard',
//         meta: { title: '控制台', icon: 'dashboard', affix: true }
//       }
//     ]
//   },

// ]

// const router = new VueRouter({
//   routes
// })



// export default router